package com.xywy.beautyand.ACT;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.xywy.beautyand.MyApplication;
import com.xywy.beautyand.R;
import com.xywy.beautyand.ADAPTER.MyCollectAdapter;
import com.xywy.beautyand.bean.MyCollectionItem;
import com.xywy.beautyand.fragment.SumQuestionTypeFrag;
import com.xywy.beautyand.util.Constants;
import com.xywy.beautyand.util.JsonUtil;
import com.xywy.beautyand.util.MD5;
import com.xywy.beautyand.util.NetUtil;
import com.xywy.beautyand.util.UIUtil;
import com.xywy.beautyand.vollery.BaseRequest;
import com.xywy.beautyand.vollery.OnVolleyResponseListener;
import com.xywy.beautyand.vollery.PostRequest;
import com.xywy.beautyand.vollery.VolleyManager;

/**
 * *****************************************  
* @Title: MyCollectionFramAct.java 
* @Package com.xywy.performgirlsandroid.act 
* @Description: TODO(我的收藏) 
* @author smz
* @date 2015-1-5 上午9:31:35 
* @version V1.0   
*******************************************
 */
public class MyCollectionFramAct extends FragmentActivity implements OnClickListener{
	
	private FragmentManager fm;
	private FragmentTransaction fragmentTransaction;
	private RelativeLayout left_img;
	private LinearLayout shaping_lin;
	private TextView shaping_text;
	private LinearLayout question_replay_lin;
	private TextView question_replay_text;
	private LinearLayout my_expert_lin;
	private TextView my_expert_text;
	private LinearLayout my_seek_lin;
	private TextView my_seek_text;
	private SumQuestionTypeFrag questionTypeFrag;
	private ListView listview;
	private List< MyCollectionItem> list;
	private MyCollectAdapter adapter;
	
	protected  ProgressDialog waittingDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_my_colleaction);
		initview();
		fm = getSupportFragmentManager();
//		switchFragment(R.id.shaping_lin);
		initclick();
		MyApplication.getInstance().addActivity(this);
		initData();
	}
    
	/**
	 * 获取收藏问题列表
	 */
	private void initData() {
		String sign;
		try {
			sign = MD5.md5s(Constants.Sign + Constants.tag);
		
		String url="http://api.yun.xywy.com/index.php/app/collect/list_data/";
		PostRequest<String> request=new PostRequest<String>(url, String.class, new OnVolleyResponseListener<String>() {

			@Override
			public void onError(VolleyError volleyError) {
				UIUtil.DissDialog(MyCollectionFramAct.this, waittingDialog);
				UIUtil.showToast(MyCollectionFramAct.this, getResources().getString(R.string.network_slow));
			}

			@Override
			public void onSuccess(String response) {
				UIUtil.DissDialog(MyCollectionFramAct.this, waittingDialog);
				try {
					JSONObject object=new JSONObject(response);
					int state=object.optInt("state");
					String error=object.optString("error");
					String data=object.optString("data");
					if(state==200){
						List<MyCollectionItem> tempData = (List<MyCollectionItem>) JsonUtil
								.parseJsonToList(
										data,
										new TypeToken<List<MyCollectionItem>>() {
										}.getType());
						if(tempData.size()>0){
							list.addAll(tempData);
							adapter.notifyDataSetChanged();
						}else{
							UIUtil.showToast(MyCollectionFramAct.this, "你还未收藏呢~");
						}
					}else{
						UIUtil.showToast(MyCollectionFramAct.this, error);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
			}
		});
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("sign", sign);
		params.put("tag", Constants.tag);
		
		params.put("xywy_userid", sign);
		params.put("app_id", "4");
		request.setParams(params);
		request.setIsParseJson(false);
		executeRequest(request);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	protected boolean executeRequest(BaseRequest<?> request) {
		if (NetUtil.hasNetwork(this)) {
			waitingDialog(this, getResources().getString(R.string.request));
			VolleyManager.addRequest(request, this);
			return true;
		} else {
			UIUtil.showToast(this, getResources().getString(R.string.not_network));
			UIUtil.DissDialog(this, waittingDialog);
			return false;
		}
	}
	
	public   void waitingDialog(Context context,String message) {
		if (waittingDialog == null)
			waittingDialog = new ProgressDialog(context);
		 waittingDialog.setCanceledOnTouchOutside(false);//�����಻��ʧ
		waittingDialog.setMessage(message);
		waittingDialog.show();
	}

	private void initview() {
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		TextView middle_title=(TextView) findViewById(R.id.middle_title);
		middle_title.setText("我的收藏");
		
		shaping_lin = (LinearLayout) findViewById(R.id.shaping_lin);
		shaping_text = (TextView) findViewById(R.id.shaping_text);
		
		question_replay_lin = (LinearLayout) findViewById(R.id.question_replay_lin);
		question_replay_text = (TextView) findViewById(R.id.question_replay_text);
		
		my_expert_lin = (LinearLayout) findViewById(R.id.my_expert_lin);
		my_expert_text = (TextView) findViewById(R.id.my_expert_text);
		
		my_seek_lin = (LinearLayout) findViewById(R.id.my_seek_lin);
		my_seek_text = (TextView) findViewById(R.id.my_seek_text);
		
		listview = (ListView)findViewById(R.id.listview);
		list=new ArrayList<MyCollectionItem>();
		adapter = new MyCollectAdapter(this, list);
		listview.setAdapter(adapter);
	}

	private void initclick() {
		left_img.setOnClickListener(this);
		shaping_lin.setOnClickListener(this);
		question_replay_lin.setOnClickListener(this);
		my_expert_lin.setOnClickListener(this);
		my_seek_lin.setOnClickListener(this);
	}
	
	/**
	 * ״̬
	 * 
	 * @param id
	 */
	private void switchFragment(int id) {
		fragmentTransaction = fm.beginTransaction();
		//
		hideFragments(fragmentTransaction);
		int fragement_textcolor_press = getResources().getColor(
				R.color.fragement_text_press);
		int fragement_textcolor_normal = getResources().getColor(
				R.color.fragement_text_normal);
		switch (id) {
		case R.id.shaping_lin:

			shaping_text.setTextColor(fragement_textcolor_press);
			question_replay_text.setTextColor(fragement_textcolor_normal);

			my_expert_text.setTextColor(fragement_textcolor_normal);

			my_seek_text.setTextColor(fragement_textcolor_normal);

			if (questionTypeFrag == null) {
				questionTypeFrag = new SumQuestionTypeFrag();
				fragmentTransaction.add(R.id.content_frame, questionTypeFrag);
			} else {
				fragmentTransaction.show(questionTypeFrag);
			}

			break;
		case R.id.question_replay_lin:
            
			shaping_text.setTextColor(fragement_textcolor_normal);
			question_replay_text.setTextColor(fragement_textcolor_press);

			my_expert_text.setTextColor(fragement_textcolor_normal);

			my_seek_text.setTextColor(fragement_textcolor_normal);
			
			if (questionTypeFrag == null) {
				questionTypeFrag = new SumQuestionTypeFrag();
				fragmentTransaction.add(R.id.content_frame, questionTypeFrag);
			} else {
				fragmentTransaction.show(questionTypeFrag);
			}

			break;

		case R.id.my_expert_lin:
			shaping_text.setTextColor(fragement_textcolor_normal);
			question_replay_text.setTextColor(fragement_textcolor_normal);

			my_expert_text.setTextColor(fragement_textcolor_press);

			my_seek_text.setTextColor(fragement_textcolor_normal);
//			if (truserFragement == null) {
//				truserFragement = new TruserBoxFragment();
//				fragmentTransaction.add(R.id.content_frame, truserFragement);
//			} else {
//				fragmentTransaction.show(truserFragement);
//			}
			break;
		case R.id.my_seek_lin:
			shaping_text.setTextColor(fragement_textcolor_normal);
			question_replay_text.setTextColor(fragement_textcolor_normal);

			my_expert_text.setTextColor(fragement_textcolor_normal);

			my_seek_text.setTextColor(fragement_textcolor_press);
			if (questionTypeFrag == null) {
				questionTypeFrag = new SumQuestionTypeFrag();
				fragmentTransaction.add(R.id.content_frame, questionTypeFrag);
			} else {
				fragmentTransaction.show(questionTypeFrag);
			}

			break;
		}
		fragmentTransaction.commit();
	}
	@Override
	public void onClick(View v) {
//		switchFragment(v.getId());
		switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;
		default:
			break;
		}
	}
	private void hideFragments(FragmentTransaction ft) {
		if (questionTypeFrag != null) {
			ft.hide(questionTypeFrag);
		}
//		if (doctorFragement != null) {
//			ft.hide(doctorFragement);
//		}
//		if (personFragement != null) {
//			ft.hide(personFragement);
//		}
//		if(truserFragement!=null){
//			ft.hide(truserFragement);
//		}
	}

}
